/**
 * 
 */
package pt.mleiria.sAction.dao.stock;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pt.mleiria.sAction.hibernate.Stock;
import pt.mleiria.sAction.hibernate.StockData;

/**
 * @author Manuel
 *
 */
public class StockDAOImpl extends HibernateDaoSupport implements StockDAO {

	/* (non-Javadoc)
	 * @see pt.mleiria.sAction.dao.stock.StockDAO#getStockList(java.lang.String, java.lang.String, java.lang.String)
	 */
	public List<Stock> getStockList(String searchTicker, String market,
			String type) throws Exception {
		List<Stock> results = new ArrayList<Stock>();
		List<Stock> sList = getHibernateTemplate().find("from Stock stock where " +
				"stock.ticker like '%" + searchTicker + "%'");
		for(Stock stock : sList){
			List<StockData> sDataList = getHibernateTemplate().find("from StockData stockData where stockData.stock = ? " + 
					" order by stockData.stockDate desc limit 0,5", stock);
			Set<StockData> set = new TreeSet<StockData>();
			/*
			for(StockData sData : sDataList){
				set.add(sData);
			}
			*/
			set.addAll(sDataList);
			stock.setStockDataSet(set);
			results.add(stock);
		}
		return results;
	}

	
}
